<?php

/**
 * calories actions.
 *
 * @package    sf_sandbox
 * @subpackage calories
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class caloriesActions extends autocaloriesActions
{
  protected function updateCaloriesFromRequest()
  {
    $calories = $this->getRequestParameter('calories');
    $this->calories->setCulture($this->getUser()->getCulture());
    if (isset($calories['cat_id']))
    {
      $this->calories->setCatId($calories['cat_id']);
    }
    if (isset($calories['name']))
    {
      $this->calories->setName($calories['name']);
    }
    if (isset($calories['default_weight']))
    {
      $this->calories->setDefaultWeight($calories['default_weight']);
    }
    if (isset($calories['kcal']))
    {
      $this->calories->setKcal($calories['kcal']);
    }
    if (isset($calories['protein']))
    {
      $this->calories->setProtein($calories['protein']);
    }
    if (isset($calories['fat']))
    {
      $this->calories->setFat($calories['fat']);
    }
    if (isset($calories['carbohydrate']))
    {
      $this->calories->setCarbohydrate($calories['carbohydrate']);
    }
  }

	public function executeIndex() 
	{
	    $this->cats = CaloriescatPeer::doSelect(new Criteria());
	}

	public function executeSelectcat() 
	{
		sfLoader::loadHelpers(array('Partial'));

		$catid = $this->getRequestParameter('catid');
		$num = $this->getRequestParameter('num');

		$c = new Criteria();
		$c->add(CaloriesPeer::CAT_ID, $catid);
		$cals = CaloriesPeer::doSelect($c);

		echo get_partial('calories/prods', array('cals' => $cals, 'num' => $num));
	    die();
	}

	public function executeKcal() 
	{
		sfLoader::loadHelpers(array('Partial'));

		$product = CaloriesPeer::retrieveByPk($this->getRequestParameter('calid'));
		$num = $this->getRequestParameter('num');
	    echo get_partial('calories/consist', array('product' => $product, 'num' => $num));
		die();
	}

	public function executeNewrow() 
	{
	    sfLoader::loadHelpers(array('Partial'));

		$num = $this->getRequestParameter('curnum');
		$num++;
		echo get_partial('calories/newrow', array('num' => $num, 'cats' => CaloriescatPeer::doSelect(new Criteria())));
		die();
	}
}
